package ranab.unit;

/**
 * This is the base class of all the unit converters. 
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public 
abstract class BaseUnit {
    
    /**
     * Get all the possible units.
     */
    public abstract String[] getAllUnits();
    
    /**
     * Convert one unit from another
     */
    public abstract String convertUnit(String value, int from, int to) throws UnitException;
    
    /**
     * Get double value.
     */
    public static double getDoubleValue(String input) throws UnitException {
        
        if(input == null) {
            throw new UnitException("Input cannot be null");
        }
        
        input = input.trim();
        if(input.equals("")) {
            throw new UnitException("Input not found"); 
        }
        
        try {
            return Double.parseDouble(input);
        }
        catch(NumberFormatException ex) {
            throw new UnitException(input + " : not a valid input");
        }
    } 
     
}